"use strict"

module.exports = function(){
	this.view = 'admin/changePass';
};
 
module.exports.submit=function(){
    this.output = 'json';
    let username = this.get('username','string');
    let passwordOld = this.get('passwordOld','string');
    let passwordNew = this.get('passwordNew','string');
    if ( !passwordOld ) {
        return this.error('请输入旧密码');
    }
    if ( !passwordNew ) {
        return this.error('新密码不能为空');
    }
    let members = this.model.admin('members');
    let keys = ['_id','xcode'];
    return members.getFromName(username,keys).then(ret=>{
        if(!ret){
            return this.error('账号不存在');
        }
        if(ret['_id']!=this.session.uid){
            return this.error('session失效，请重新登录');
        }
        let verify_xcode = members.password(passwordOld);
        if( verify_xcode !== ret['xcode']){
            return this.error('password error');
        }
        const _query={'guid':username};
        const _update={'$set':{'xcode':members.password(passwordNew)}};
        return members.update(_query,_update,{});
    }).then(ret=>{
            return this.session.del().then(()=>{
        });
    });
};
